using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class forums_controls_usercp_profile : Controls.DataManagerUserControl
{
    protected override void ItemChanged()
    {
        Refresh();
    }

    protected override void UserLoggedIn(object sender, EventArgs e)
    {
        Refresh();
    }

    protected override void UserLoggedOut(object sender, EventArgs e)
    {
        Refresh();
    }

    private void Refresh()
    {
        if (CurrentUser != null)
        {
            mviewProfile.SetActiveView(viewProfile);
            Page.Title = "FSTDT Forums: " + Helpers.StringHelper.SafeFormat("{0}'s profile", CurrentUser.Username);

            lblUsername.Text = Helpers.StringHelper.SafeFormat(CurrentUser.Username);

            tdAvatar.Visible = CurrentUser.HasAvatar && CurrentUser.AdminSettings.AllowAvatar;
            imgAvatar.ImageUrl = Helpers.StringHelper.FormatIfExists("~/forums/avatars/{0}.{1}", CurrentUser.ID, CurrentUser.AvatarExtension);

            pnlUsertitle.Visible = CurrentUser.HasUserTitle && CurrentUser.AdminSettings.AllowUserTitle;
            lblUsertitle.Text = Helpers.StringHelper.SafeFormat(CurrentUser.UserTitle);

            lblDateRegistered.Text = CurrentUser.RegistrationDate.ToString("yyyy-MMM-dd");

            tdSignature.Visible = CurrentUser.HasSignature && CurrentUser.AdminSettings.AllowSignature;
            lblSignature.Text = string.Format("<blockquote><hr /><p>{0}</p><hr /></blockquote>", DAL.YCodes.Format(CurrentUser.Signature));

            if (String.IsNullOrEmpty(CurrentUser.Location))
            {
                lblLocation.Visible = false;
            }
            else
            {
                lblLocation.Visible = true;
                lblLocation.Text = Helpers.StringHelper.SafeFormat("<p>Location: {0}</p>", CurrentUser.Location);
            }

            if (String.IsNullOrEmpty(CurrentUser.Bio))
            {
                lblBio.Visible = false;
            }
            else
            {
                lblBio.Visible = true;
                lblBio.Text = String.Format("<p>Bio: {0}</p>", DAL.YCodes.Format(CurrentUser.Bio));
            }

            if (String.IsNullOrEmpty(CurrentUser.Interests))
            {
                lblInterests.Visible = false;
            }
            else
            {
                lblInterests.Visible = true;
                lblInterests.Text = String.Format("<p>Interests: {0}</p>", DAL.YCodes.Format(CurrentUser.Interests));
            }

            lblPosts.Text = CurrentUser.TotalPosts.ToString();
            lnkAllPosts.NavigateUrl = Helpers.StringHelper.SafeFormat("~/forums/searchresults.aspx?user={0}&userfilter=1&sort=0&sortdir=1&display=1", CurrentUser.ID);
            lblThreads.Text = CurrentUser.TotalThreads.ToString();
            lnkAllThreads.NavigateUrl = Helpers.StringHelper.SafeFormat("~/forums/searchresults.aspx?user={0}&userfilter=0&sort=0&sortdir=1&display=0", CurrentUser.ID);

            if (String.IsNullOrEmpty(CurrentUser.Occupation))
            {
                lblOccupation.Visible = false;
            }
            else
            {
                lblOccupation.Visible = true;
                lblOccupation.Text = Helpers.StringHelper.SafeFormat("<p>Occupation: {0}</p>", CurrentUser.Occupation);
            }

            if (String.IsNullOrEmpty(CurrentUser.Website) || !Uri.IsWellFormedUriString(CurrentUser.Website, UriKind.Absolute))
            {
                lblWebsite.Visible = false;
            }
            else
            {
                lblWebsite.Visible = true;
                lblWebsite.Text = Helpers.StringHelper.SafeFormat("<p>Website: <a href=\"{0}\" target=\"_blank\">{0}</a></p>", CurrentUser.Website);
            }

            if (Managers.UserLoginManager.CurrentUser != null)
            {
                lnkSendMessage.Visible = true;
                lnkSendMessage.Text = Helpers.StringHelper.SafeFormat("Send {0} a private message", CurrentUser.Username);
                lnkSendMessage.NavigateUrl = Helpers.StringHelper.SafeFormat("~/forums/usercp.aspx?newpm={0}", CurrentUser.ID);
            }
            else
            {
                lnkSendMessage.Visible = false;
            }
        }
        else
        {
            mviewProfile.SetActiveView(viewError);
            Page.Title = "FSTDT Forums: " + "Oops!";
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            CurrentUser = GetItemFromQueryString();
        }
    }
}
